package com.hospital.service.impl;

import com.alibaba.excel.EasyExcel;
import com.hospital.dao.EmployeeDao;
import com.hospital.dto.EmployeeExportDto;
import com.hospital.entity.Employee;
import com.hospital.service.intf.EmployeeService;
import com.hospital.util.StringUtil;
import com.hospital.vo.BarVo;
import com.hospital.vo.JovPieVo;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public R add(Employee employee) {
        if(employeeDao.add(employee) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R delete(Integer id) {
        if(employeeDao.delete(id) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R update(Employee employee) {
        if(employeeDao.update(employee) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageBean selectPage(String name,String job_name,int page, int limit) {
        int start = (page - 1) * limit;

        if(name != null){
            name = "%"+name+"%";
        }


        if(job_name != null){
            job_name = "%"+job_name+"%";
        }

        return PageBean.ok(employeeDao.count(name,job_name),employeeDao.selectPage(name,job_name,start,limit));
    }

    @Override
    public R deleteByIds(String ids) {

        String[] id = ids.split(",");

        if(employeeDao.deleteByIds(id) > 0){
            return R.ok();
        }

        return R.fail();
    }

    @Override
    public List<BarVo> sexBarData() {
        return employeeDao.sexBarData();
    }

    @Override
    public List<JovPieVo> jobPieData() {
        return employeeDao.jobPieData();
    }

    @Override
    public List<EmployeeExportDto> all() {
        return employeeDao.all();
    }

    @Override
    public R selectById(Integer id) {
        if(employeeDao.selectById(id) != null){
            return R.ok(employeeDao.selectById(id));
        }
        return R.fail();
    }




}
